﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Security.Principal;
using System.ServiceModel;
using Microsoft.SharePoint;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Navigant.DataContracts;
using Navigant.ServiceInterfaces;

namespace Navigant.UnitTests.WebServices
{
    [TestClass()]
    public class CustodianServiceTests
    {
        public TestContext TestContext { get; set; }
        private string portalUrl;

        public CustodianServiceTests()
        {
            this.portalUrl = ConfigurationManager.AppSettings["testCaseSite"];
        }

        private class CustodianClient : ClientBase<ICustodianService>, ICustodianService
        {
            public CustodianClient()
            {
                this.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
            }

            #region ICustodianService Members

            public List<CustodianContract> GetCustodians(CaseContract parentCase)
            {
                return this.Channel.GetCustodians(parentCase);
            }

            public void ImportCustodians(CaseContract parentCase, List<CustodianContract> custodians)
            {
                this.Channel.ImportCustodians(parentCase, custodians);
            }

            public List<CustodianContract> GetCustodiansWithExtraCriteria(CaseContract parentCase,
                string extraCriteriaFieldName, string extraCriteriaFieldValue)
            {
                return this.Channel.GetCustodiansWithExtraCriteria(parentCase, extraCriteriaFieldName, extraCriteriaFieldValue);
            }

            public List<string> GetExtraCriteriaPossibleValues(CaseContract parentCase,
                string extraCriteriaFieldName)
            {
                return this.Channel.GetExtraCriteriaPossibleValues(parentCase, extraCriteriaFieldName);
            }

            #endregion
        }

        [TestMethod()]
        public void GetCustodiansFromService()
        {
            CaseContract parentCase = new CaseContract();
            using (SPSite site = new SPSite(this.portalUrl))
            {
                parentCase.ClientPortalId = site.ID;
                using (SPWeb web = site.OpenWeb())
                {
                    parentCase.Id = web.ID;
                    parentCase.Name = web.Title;
                }
            }
            ICustodianService service = new CustodianClient();
            List<CustodianContract> custodians = service.GetCustodians(parentCase);
            Assert.IsTrue(custodians.Count > 0);
        }
    }
}
